package taskscheduleservicelogic

import (
	"context"
	"github.com/jinzhu/copier"
	"go-zero-admin/application/job/rpc/internal/model"
	"go-zero-admin/application/job/rpc/internal/svc"
	job "go-zero-admin/application/job/rpc/pb"
	"go-zero-admin/pkg/utils"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetCronInfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetCronInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCronInfoLogic {
	return &GetCronInfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetCronInfo 根据任务id查询任务信息
func (l *GetCronInfoLogic) GetCronInfo(in *job.GetCronInfoReq) (*job.GetCronInfoRes, error) {
	var mo model.TaskSchedule
	if err := l.svcCtx.DB.Where("id = ?", in.TaskId).First(&mo).Error; err != nil {
		return nil, err
	}
	info := &job.TaskScheduleInfo{}
	_ = copier.Copy(&info, &mo)
	info.CreatedAt = utils.SafeUnix(&mo.CreatedAt)
	info.UpdatedAt = utils.SafeUnix(&mo.UpdatedAt)
	info.LastExecuteTime = utils.SafeUnix(mo.LastExecuteTime)

	return &job.GetCronInfoRes{
		TaskScheduleInfo: info,
	}, nil
}
